
/*
*Released on 28th June 2008.
*Any violations can be reported at paritosh@wikiocean.net
*What is treated as violations can be found at www.wikiocean.net/ppl1-voilations*
******************************************************************************
* The contents of this file are subject to POOL Public License 1.0 and later.
* POOL Public License is based on Affero GPL v3 and additional conditions.
* ("License"); You may not use this file except in compliance with the License
* You may obtain a copy of the Affero GPL v3 License at http://www.gnu.org/licenses/agpl.html
* You may obtain a copy of the POOL Public License 1.0 or later at www.wikiocean.net/license/ppl.html
* Software distributed under POOL Public License 1.0 is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
* The Initial Developer of the Original Code is Paritosh Pungaliya (C) 2008. All Rights Reserved.
******************************************************************************
* Objective of the additional terms (license)
* 1) Is to extend the software freedom to freedom to work.
* 2) To ensure that knowledge is free from monopoly of agencies.
* 3) To avoid a situation where big corporate or investor can buy out free software groups and companies and then start milking the communities built around it.
* (this trend can be seen how the freedom is curtailed in companies that get Venture Capital funding.)
******************************************************************************
*/

package  basicprocess;

import java.util.Hashtable;
import java.util.Vector;

import control.RequestParameter;

import property.Property;
import pool.tablename.Pool;
import utility.CapitalChar;
import utility.Input;
import database.RecordSearch;
import dbmanager.Column;
import dbmanager.DBManager;
import dbmanager.GetIdName;


public class Authentication implements Basicprocessinterface  
{
	public Pool pool;
	public DBManager database;
	String USER="user";
	String PERSONCODE="personcode";
	String USERID="userid";
	GetIdName gid;
	RecordSearch sr;
	String type;
	public RequestParameter req;
	String AUTHENTICATION = "authentication"; 
	String MASTER="master";
	String GROUP="group";
	Hashtable<Object, Object> Result = new Hashtable<Object, Object>();
	String AUTENTICATIONERROR="ErrorInput";
	Hashtable<Object, Object> Error =new Hashtable<Object, Object>();
	public Hashtable<Object, Object> MasterMap =new Hashtable<Object, Object>();
	public Vector<Object> PropertyMap  =new Vector<Object>();
	public String TYPE="type";
	public String tablename;
	
	public void getInsert() 
	{
		
	}

	public void getUpdate() 
	{
		
	}

	public void processBasicprocess() 
	{
		try 
		{
			String Userid=  req.getUsername();
			Userid = gid.getId(Userid);
			Hashtable<Object, Object> table =new Hashtable<Object, Object>();
			table.put("mid", Userid);
			table.put("pid",AUTHENTICATION);
			sr.setConditionMap(table);
			String line[] = sr.getArrayFromResultSet();	
			table.clear();
			table.put("mid", Userid);
			table.put("td", "null");
			table.put("pid", GROUP);
			sr.setConditionMap(table);
			String temp[] = sr.getArrayFromResultSet();
			String result[]={};
			for(int i=0;i<temp.length;i++)
			{
				String splitline[]=temp[i].split("\t");
				table.clear();
				table.put("mid", splitline[Column.pv_index-1]);
				table.put("pid", AUTHENTICATION);
				table.put("td", "null");
				sr.setConditionMap(table);
				result = sr.getArrayFromResultSet();
			}
			
			temp  = new String[result.length+line.length];
			for(int i=0;i<result.length;i++)
			{
				temp[i]=result[i];
			}
			for(int i=0;i<line.length;i++)
			{
				temp[i]=line[i];
			}
			
			line = temp;
			String splitline[] = line[0].split("\t");
			
			table.clear();
			table.put("mid",splitline[Column.pv_index-1]);
			table.put("pid", type);
			table.put("td", "null");
			sr.setConditionMap(table);
			line = sr.getArrayFromResultSet();
			if(line.length==0)
				Error.put(AUTENTICATIONERROR, "You are not authorized to  "+req.getProcess()+" "+gid.getItem(type));
			for(int i=0;i<line.length;i++)
			{
				splitline = line[i].split("\t");
				basicprocess.authentication.Authentication authenticationonject;
				try 
				{	
					Property gp =new Property();
					gp.setMid(splitline[Column.pv_index-1]);
					gp.setPid(TYPE);
					String classname= gp.getValue();	
					Class c = Class.forName("basicprocess.authentication."+CapitalChar.makeFirstCharCapital(classname));
					authenticationonject = (basicprocess.authentication.Authentication) c.newInstance();
					authenticationonject.setobject(this, splitline[Column.pv_index-1]);					
					if(authenticationonject.getResult())
					{	
						Error.put(AUTENTICATIONERROR, "You are not authorized to  "+pool.getRequestParameter().getProcess()+" "+gid.getItem(type));
						break;
					}	
					else
					{
						Error.clear();
						//break;
					}	
				}
				catch (ClassNotFoundException e) 
				{
					e.printStackTrace();
				} 
				catch (InstantiationException e) 
				{
					e.printStackTrace();
				}
				catch (IllegalAccessException e) 
				{
					e.printStackTrace();
				}
			}
		} catch (Exception e) 
		{	
			e.printStackTrace();
			Error.put(AUTENTICATIONERROR, "You are not authorized to  "+req.getProcess()+" "+gid.getItem(type));
		
		}
	}

	public void Init() 
	{
		gid =new GetIdName();
		AUTHENTICATION = gid.getId(AUTHENTICATION);
		MASTER =  gid.getId(MASTER);
		USERID = gid.getId(USERID);
		PERSONCODE = gid.getId(PERSONCODE);
		GROUP= gid.getId(GROUP);
		TYPE = gid.getId(TYPE);
		sr =new RecordSearch(); 
	}

	public void setObject(Object obj) 
	{
		pool = (Pool) obj;
	}
	public Hashtable<Object, Object> getHashtable()
	{
		return Error;
	}
	public boolean checkacess(String rights)
	{
		String Request =req.getProcess();
		String requestfirstchar = Request.substring(0, 1);
		if(rights.contains(requestfirstchar))
			return true;
		else
			return false;
	}

	public void setMasterHashtabel(Hashtable<Object, Object> MasterMap)
	{
		this.MasterMap.putAll(MasterMap);
	}

	public void setpropertydetailsVector(Vector<Object> PropertyMap) 
	{
		this.PropertyMap.addAll(PropertyMap);
	}

	public void setRequestParameter(RequestParameter req) 
	{
		this.req = req;
	}

	public void setType(String type) 
	{
		this.type = type;
	}

	public void setTablename(String tablename) 
	{
		this.tablename =tablename;
	}
}
